package com.ebocy.hammer.game.handler;

import java.util.List;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.ebocy.hammer.core.GameCache;
import com.ebocy.hammer.core.constants.GameMessageCode;
import com.ebocy.hammer.core.domain.GamePlayer;
import com.ebocy.hammer.core.domain.GamePlayerItem;
import com.ebocy.hammer.core.handler.IMessageHandler;
import com.ebocy.hammer.core.log.EbocyLogManager;
import com.ebocy.hammer.core.message.IMessage;
import com.ebocy.hammer.core.service.GamePlayerService;
import com.ebocy.hammer.core.service.ItemService;
import com.ebocy.hammer.core.util.Config;
import com.ebocy.hammer.game.message.C2SEnterGameHomePageMessage;
import com.ebocy.hammer.game.message.C2SItemChange;
import com.ebocy.hammer.game.message.C2SPlayerRole;
import com.ebocy.hammer.game.message.C2SShopOperation;
import com.ebocy.hammer.game.message.S2CEnterGameHomePageMessage;
import com.ebocy.hammer.game.message.S2CClickTeamCombatMessage;
import com.ebocy.hammer.game.service.GameServerService;

/**
 * 战斗逻辑处理
 *
 * @author Qingrong Hou Jun 14, 2012
 */
public class GameServerHandler implements IMessageHandler {
	private static Logger logger = EbocyLogManager.getSystemLogger();
	private short[] messageCode = {
			GameMessageCode.HERT_BEAT,
			GameMessageCode.GAME_ENTER_HOME_PAGE,
			GameMessageCode.GAME_CLICK_TEAM_COMBAT,
			GameMessageCode.GAME_CHANGE_ROLE,
			GameMessageCode.LOGOUT,
			GameMessageCode.GAME_SHOP_OPERATION,
			GameMessageCode.GAME_ITEM_CHANGE
		};
	private static GameServerHandler instance = null;
	private GameServerService gameServerService = GameServerService.getInstance();
	private GamePlayerService gamePlayerService = GamePlayerService.getInstance();

	public static GameServerHandler getInstance() {
		if (instance == null)
			instance = new GameServerHandler();
		return instance;
	}

	@Override
	public short[] getMessageCodes() {
		return messageCode;
	}

	@Override
	public void resolve(IMessage message, IoSession session) {
		GamePlayer gamePlayer = GameCache.getInstance().getGamePlayerBySession(session);
		switch (message.getMessageCode()) {
		case GameMessageCode.HERT_BEAT:
			if (gamePlayer != null) {
				gamePlayer.heartBeatTimestamp = System.currentTimeMillis();
			}
			break;
		case GameMessageCode.GAME_ENTER_HOME_PAGE:
			C2SEnterGameHomePageMessage enterHomePageMessage = (C2SEnterGameHomePageMessage) message;
			gamePlayer = gamePlayerService.getGamePlayerById(enterHomePageMessage.getGamePlayerId());
			//获取用户背包信息
			List<GamePlayerItem> knapsack =ItemService.getInstance().getKnapsack(gamePlayer.getGamePlayerId());
			gamePlayer.setKnapsack(knapsack);
			gamePlayer.heartBeatTimestamp=System.currentTimeMillis();
			gamePlayer.setSession(session);
			GameCache.getInstance().addGamePlayer(gamePlayer);
			gamePlayer.sendMessage(new S2CEnterGameHomePageMessage());
			break;
		case GameMessageCode.GAME_CLICK_TEAM_COMBAT:
			S2CClickTeamCombatMessage teamCombatMessage = new S2CClickTeamCombatMessage(Config.worldServer.getIp(), Config.worldServer.getPort());
			gamePlayer.sendMessage(teamCombatMessage);
			break;
		case GameMessageCode.GAME_CHANGE_ROLE:
			C2SPlayerRole c2SPlayerRole = (C2SPlayerRole) message;
			gameServerService.changeRole(gamePlayer, c2SPlayerRole);
			break;
		case GameMessageCode.GAME_SHOP_OPERATION:
			C2SShopOperation c2SShop = (C2SShopOperation) message;
			gameServerService.shopOperation(gamePlayer, c2SShop);
			break;
		case GameMessageCode.GAME_ITEM_CHANGE:
			C2SItemChange c2SItemChange = (C2SItemChange) message;
			gameServerService.itemChange(gamePlayer, c2SItemChange);
			break;
		case GameMessageCode.LOGOUT:
			gamePlayerService.exitGame(gamePlayer);
			break;
		default:
			logger.error("code" + message.getMessageCode() + "未处理！");
			break;
		}
	}

}
